library(ggplot2)
library(ggthemes)
library(reshape2)

p1 <- ggplot(diamonds, aes(carat)) +
  geom_histogram()
p1

p2 <- ggplot(diamonds, aes(carat)) +
  geom_histogram(aes(y = ..density..))
p2

# 1. Histogram
set.seed(1234)
df <- data.frame(sex = rep(c("F", "M"), each = 200),
                 weight = c(rnorm(200, mean = 55, sd = 5),
                            rnorm(200, mean = 65, sd = 5)))

ggplot(df, aes(weight)) + geom_histogram()
ggplot(df, aes(weight)) + geom_histogram(binwidth = 1)
ggplot(df, aes(weight)) + 
    geom_histogram(color = "black", fill = "white")

# add mean line
p <- ggplot(df, aes(weight)) + 
    geom_histogram(color = "black", fill = "white")
p + geom_vline(aes(xintercept = mean(weight)),
               color = "blue", linetype = "dashed", size = 1)

# add density
ggplot(df, aes(x = weight)) +
    geom_histogram(aes(y = ..density..), color = "black", fill = "white") +
    geom_density(alpha = 0.2, fill = "#FF6666")

# change line type and colors
ggplot(df, aes(weight)) +
    geom_histogram(color = "darkblue", fill = "lightblue")
ggplot(df, aes(weight)) +
    geom_histogram(color = "black", fill = "lightblue", linetype = "dashed")

# histogram by groups
library(dplyr)
mu <- df %>% group_by(sex) %>% 
    summarise(grp.mean = mean(weight))

ggplot(df, aes(weight, color = sex)) +
    geom_histogram(fill = "white", alpha = 0.5, 
                   position = "identity")  # overlaid

# interleaved
ggplot(df, aes(weight, color = sex)) +
    geom_histogram(fill = "white", position = "dodge") +
    theme(legend.position = "top")

p <- ggplot(df, aes(weight, color = sex)) +
    geom_histogram(fill = "white", position = "dodge") +
    geom_vline(data = mu, aes(xintercept = grp.mean, color = sex), linetype = "dashed") +
    theme(legend.position = "top")
p

# Use custom color palettes
p + scale_color_manual(values = c("#999999", "#E69F00", "#56B4E9")) +
    scale_fill_manual(values = c("#999999", "#E69F00", "#56B4E9"))
# use brewer color palettes
p + scale_color_brewer(palette = "Dark2") +
    scale_fill_brewer(palette = "Dark2")
# Use grey scale
p + scale_color_grey() + scale_fill_grey() +
    theme_classic()

# Change fill colors

# Change histogram plot fill colors by groups
ggplot(df, aes(x = weight, fill = sex, color = sex)) +
    geom_histogram(position = "identity")
# Use semi-transparent fill
p <- ggplot(df, aes(x = weight, fill = sex, color = sex)) +
    geom_histogram(position = "identity", alpha = 0.5)
p
# Add mean lines
p + geom_vline(data = mu,
               aes(xintercept = grp.mean, color = sex),
               linetype = "dashed")

# Use custom color palettes
p + scale_color_manual(values = c("#999999", "#E69F00", "#56B4E9")) +
    scale_fill_manual(values = c("#999999", "#E69F00", "#56B4E9"))
# use brewer color palettes
p + scale_color_brewer(palette = "Dark2") +
    scale_fill_brewer(palette = "Dark2")
# Use grey scale
p + scale_color_grey() + scale_fill_grey() +
    theme_classic()

# Change the legend position
p + theme(legend.position = "top")
p + theme(legend.position = "bottom")
# Remove legend
p + theme(legend.position = "none")

# Facets
p <- ggplot(df, aes(x = weight)) +
    geom_histogram(color = "black", fill = "white") +
    facet_grid(sex ~ .)
p
# Add mean lines
p + geom_vline(data = mu,
               aes(xintercept = grp.mean, color = "red"),
               linetype = "dashed")

# Customized histogram plots
# Basic histogram
ggplot(df, aes(x = weight, fill = sex)) +
    geom_histogram(fill = "white", color = "black") +
    geom_vline(aes(xintercept = mean(weight)), color = "blue",
               linetype = "dashed") +
    labs(title = "Weight histogram plot", x = "Weight(kg)", y = "Count") +
    theme_classic()
# Change line colors by groups
ggplot(df, aes(x = weight, color = sex, fill = sex)) +
    geom_histogram(position = "identity", alpha = 0.5) +
    geom_vline(data = mu,
               aes(xintercept = grp.mean, color = sex),
               linetype = "dashed") +
    scale_color_manual(values = c("#999999", "#E69F00", "#56B4E9")) +
    scale_fill_manual(values = c("#999999", "#E69F00", "#56B4E9")) +
    labs(title = "Weight histogram plot", x = "Weight(kg)", y = "Count") +
    theme_classic()

# Change line colors by groups
ggplot(df, aes(x = weight, color = sex, fill = sex)) +
    geom_histogram(aes(y = ..density..), position = "identity", alpha =
                       0.5) +
    geom_density(alpha = 0.6) +
    geom_vline(data = mu,
               aes(xintercept = grp.mean, color = sex),
               linetype = "dashed") +
    scale_color_manual(values = c("#999999", "#E69F00", "#56B4E9")) +
    scale_fill_manual(values = c("#999999", "#E69F00", "#56B4E9")) +
    labs(title = "Weight histogram plot", x = "Weight(kg)", y = "Density") +
    theme_classic()

# Change line colors manually :
p <- ggplot(df, aes(x = weight, color = sex)) +
    geom_histogram(fill = "white", position = "dodge") +
    geom_vline(data = mu,
               aes(xintercept = grp.mean, color = sex),
               linetype = "dashed")
# Continuous colors
p + scale_color_brewer(palette = "Paired") +
    theme_classic() + theme(legend.position = "top")
# Discrete colors
p + scale_color_brewer(palette = "Dark2") +
    theme_minimal() + theme_classic() + theme(legend.position = "top")
# Gradient colors
p + scale_color_brewer(palette = "Accent") +
    theme_minimal() + theme(legend.position = "top")


# 2. Histogram::theme
sdm <- diamonds[sample(nrow(diamonds), 1000), ]

ggplot(sdm) + geom_histogram(aes(x = price))
ggplot(sdm, aes(price)) + geom_histogram()
ggplot(sdm, aes(price, fill = cut)) + geom_histogram()

ggplot(sdm, aes(price, fill = cut)) + geom_histogram(position = 'stack')

ggplot(sdm, aes(price, fill = cut, alpha = 1/10)) + 
  geom_histogram(position = 'identity')

ggplot(diamonds, aes(carat)) +
  geom_histogram(binwidth = 0.1) +
  theme_stata() + scale_fill_stata()

ggplot(diamonds, aes(carat)) +
  geom_histogram(binwidth = 0.1) +
  theme_solarized() + scale_fill_solarized()

ggplot(diamonds, aes(carat, fill = "steelblue")) +
  geom_histogram(binwidth = 0.1) +
  theme_few() +
  scale_fill_manual(values = "#FB882C") + 
  theme(strip.background = element_blank(), legend.position = "none") 

ggplot(sdm, aes(price, fill = cut)) +
  geom_histogram(position = "fill") +
  theme_wsj() +
  scale_fill_wsj() +
  theme(strip.background = element_blank(), legend.position = "none")

ggplot(sdm, aes(price, fill = cut)) +
  geom_histogram(position = "fill") +
  theme_economist(base_size = 14) +
  scale_fill_economist() +
  theme(strip.background = element_blank(), legend.position = "none")

ggplot(sdm, aes(price, fill = cut)) +
  geom_histogram() + facet_wrap(~cut) +
  theme_wsj() + scale_fill_wsj() + guides(fill = guide_legend(title = NULL))
